﻿using System;
using System.Collections.Generic;
using System.Web.Http;
using AttributeRouting.Web.Http;
using WS.EKA.Core.DI;
using WS.EKA.Model;
using WS.EKA.ServiceInterface;
using WS.EKA.Utilities.Consts;
using WS.EKA.Portal.Filters;
using System.Text;
using WS.EKA.Utilities.Helpers;

namespace WS.EKA.Portal.Controllers
{
    [AuthencationFilter(true)]
    public class ProductController : ControllerBase
    {
        private IServiceFactory factory;
        private IProduct product;
        private IProductComment productComment;
        private IBaskOrderLog baskOrderLog;
        private IProductBrand productBrand;

        public ProductController(IServiceFactory factory)
        {
            this.factory = factory;
            product = factory.GetInterfaceImplement<IProduct>();
            productComment = factory.GetInterfaceImplement<IProductComment>();
            baskOrderLog = factory.GetInterfaceImplement<IBaskOrderLog>();
            productBrand = factory.GetInterfaceImplement<IProductBrand>();
        }

        [GET("api/product/namelist")]
        public List<SimpleProduct> GetProductNameList()
        {
            return product.GetProductNameList();
        }
        [GET("api/product/list/{ProductCategoryID}")]
        public Dictionary<string, Object> GetProductList(int ProductCategoryID, int pageIndex, int pageCount, string sorts, bool isASC)
        {
            var result = product.GetProductListByCondition(ProductCategoryID, pageIndex, pageCount,
                StrongQuickEnum<ProductSort>.Parse(sorts, true), isASC);
            return result;
        }

        [GET("api/product/{id}")]
        public Product GetProductByID(Guid id)
        {
            return product.GetProductByID(id);
        }

        [GET("api/product/search/{ProductCategoryID}")]
        public Dictionary<string, Object> GetProductListByName(int ProductCategoryID, int pageIndex, int pageCount,
            string sorts, bool isASC, string name)
        {
            return product.GetProductListByName(ProductCategoryID, pageIndex, pageCount,
                StrongQuickEnum<ProductSort>.Parse(sorts, true), isASC, name);
        }

        [GET("api/product/comment/{productID}")]
        public Dictionary<string, Object> GetProductCommentList(int startPage, int pageSize, Guid productID)
        {
            return productComment.GetProductCommentList(startPage, pageSize, productID);
        }

        [GET("api/product/baskorderlog/{productID}")]
        public Dictionary<string, Object> GetBaskOrderLogs(int startPage, int pageSize, Guid productID)
        {
            var result = baskOrderLog.GetBaskOrderLogs(startPage, pageSize, productID);
            return result;
        }

        [GET("api/product/baskorderlogdetail/{baskOrderLogID}")]
        public BaskOrderLogAndComments GetBaskOrderLogAndComments(Guid baskOrderLogID)
        {
            var result = baskOrderLog.GetBaskOrderLogAndComments(baskOrderLogID);
            return result;
        }

        [GET("api/product/filter/{ProductCategoryID}")]
        public Dictionary<string, Object> GetProductListByFilter(int ProductCategoryID, int pageIndex, int pageCount,
            string sorts, bool isASC, decimal minPrice, decimal maxPrice, string brand)
        {
            if (minPrice == 0 && maxPrice == 0 && string.IsNullOrEmpty(brand))
            {
                StringBuilder sb = new StringBuilder();
                sb.Append(GetCurrentUser()+":");
                sb.Append("price zone and brand name must not be null at the same time when filtering.");
                throw new Exception(sb.ToString());
            }
            return product.GetProductListByPriceZoneAndBrand(ProductCategoryID, pageIndex, pageCount,
                StrongQuickEnum<ProductSort>.Parse(sorts, true), isASC, minPrice, maxPrice, brand);
        }

        [GET("api/product/brandlist/{productCategoryID}")]
        public List<ProductBrandRelation> GetProductBrand(int productCategoryID)
        {
            var result = productBrand.Get(productCategoryID);
            return result;
        }
    }
}
